<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\adapay\model;

class AccountsRecord extends \app\BaseModel
{
    protected $name = "shequshop_adapay_accounts_record";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateOne($id)
    {
        $user_id = $this->where(["id" => $id])->value("user_id");
        $res = $this->where(["user_id" => $user_id])->where("id", "<>", $id)->update(["status" => 0]);
        return $res;
    }
    public function giveMemberCash($member_id, $point, $uniacid, $true_cash)
    {
        $adapay_model = new \longbingcore\wxcore\Adapay($uniacid);
        $member_model = new Member();
        $bank_model = new Bank();
        $admin_pay_model = new AdminPay();
        $config_model = new Config();
        $acc_record_model = new AccountsRecord();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $member = $member_model->dataInfo(["id" => $member_id]);
        if(!empty($member)) {
            $bank = $bank_model->dataInfo(["order_member_id" => $member["id"], "status" => 1]);
            if(!empty($bank)) {
                $acct = $adapay_model->settleAccount($bank["member_id"], $bank["settle_account_id"]);
                if(!isset($acct["avl_balance"])) {
                    return ["code" => 500, "msg" => "账户信息错误"];
                }
                if($acct["avl_balance"] < $true_cash) {
                    $del_cash = $true_cash - $acct["avl_balance"];
                    $adapay_balance_point = getConfigSetting($uniacid, "adapay_balance_point");
                    $del_cash = round($del_cash / (100 - $adapay_balance_point) * 100, 2);
                    $res = $adapay_model->balancePay($bank["member_id"], $del_cash);
                    if(!empty($res["status"]) && $res["status"] == "succeeded") {
                        $insert = ["uniacid" => $uniacid, "cash" => $true_cash - $acct["avl_balance"], "pay_id" => 0, "member_id" => $member["id"], "bank_id" => $bank["id"], "adapay_member_id" => $bank["member_id"], "settle_account_id" => $bank["settle_account_id"], "adapay_id" => $res["order_no"]];
                        $acc_record_model->dataAdd($insert);
                    } else {
                        $admin_cash = $admin_pay_model->whereTime("pay_time", "-547 days")->where("true_price", ">", 0)->where(["pay_type" => 2])->sum("true_price");
                        if($admin_cash + $acct["avl_balance"] < $true_cash) {
                            return ["code" => 500, "msg" => "账户资金不足,请先充值"];
                        }
                        $del_cash = $true_cash - $acct["avl_balance"];
                        $del_cash = round($del_cash / (100 - $point) * 100, 2);
                        $list = $admin_pay_model->whereTime("pay_time", "-547 days")->where("true_price", ">", 0)->where(["pay_type" => 2])->order("id desc")->select()->toArray();
                        if(!empty($list)) {
                            foreach ($list as $value) {
                                $d_cash = $del_cash < $value["true_price"] ? $del_cash : $value["true_price"];
                                $del_cash -= $d_cash;
                                if(0 < $d_cash) {
                                    $div_members = ["member_id" => $bank["member_id"], "amount" => sprintf("%01.2f", $d_cash)];
                                    if($config["commission"] == 1) {
                                        $div_members["fee_flag"] = "Y";
                                    }
                                    $res = $adapay_model->confirmorderCreate($value["adapay_id"], orderCode(), $d_cash, [$div_members], "账户余额不足，平台分账", $config["commission"]);
                                    if($res["status"] == "succeeded") {
                                        $insert = ["uniacid" => $uniacid, "cash" => $d_cash, "pay_id" => $value["id"], "member_id" => $member["id"], "bank_id" => $bank["id"], "adapay_member_id" => $bank["member_id"], "settle_account_id" => $bank["settle_account_id"], "adapay_id" => $res["id"]];
                                        $acc_record_model->dataAdd($insert);
                                        $admin_pay_model->where(["id" => $value["id"]])->update(["true_price" => \think\facade\Db::raw("true_price-" . $d_cash)]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}

?>